$(function() {
    var id = tools.queryString().id || 1;
    //获取新闻详情
    function getNewsDetail(id) {
        $.ajax({
            type: "get",
            // url:domain.test + "/getnewsdetail",
            url:domain.online + "/api/news/get",
            data: {id: id},
            success: function(data) {
                // if(data.data.rtnCode === 200) {
                if(data.rtnCode === 200) {
                    // var newsDetail = data.data.data;
                    var newsDetail = data.data;
                    document.title = newsDetail.title;
                    if (newsDetail.type === 1) {
                        $(".newsArticle").removeClass("hideNewsArticle");
                        $($(".newItem")[0]).css({ "borderTop": "1px solid #ebebeb" });
                        $(".newsArticle").html(template("newsArticleText", { newsArticlMsg: newsDetail }));
                    } else if (newsDetail.type === 2) {
                        $(".videoContainer").removeClass("hideVideoContainer");
                        $(".videoContainer").html(template("videoMsg", { videoMsg: newsDetail }));
                        $(".recommendNews").html(template("recommendNewsTpl", { newsList: newsDetail.list }));
                    }
                }
            }
        });
    }
    getNewsDetail(id);
    //推荐新闻点击跳转
    $(".recommendNews").on("click", ".newItem", function() {
        var id = $(this).attr("newsId");
        location.href = location.href.slice(0, -1) + id;
    });
    var pageNum = 1;
    window.total = 0;
    window.commentList = [];
    //新闻评论
    function getNewsComment(id, pageNum, commentList) {
        $.ajax({
            type: "get",
            // url:domain.test + "/getnewscomments",
            url:domain.online + "/api/news/review",
            data: {id: id,pageNum: pageNum},
            success: function(data) {
                // if(data.data.rtnCode === 200) {
                if(data.rtnCode === 200) {
                    // var commentDetail = data.data.data;
                    var commentDetail = data.data;
                    window.commentList = commentList.concat(commentDetail.list);
                    window.total = commentDetail.total;
                    $(".newsComments").html(template("userComment", { commentList: window.commentList }));
                }
            }
        });
    }
    getNewsComment(id, pageNum, window.commentList);
    $(".newsComments").on("click",".moreCommentsBtn",function() {
        if (window.total > 1) {
            pageNum++;
            getNewsComment(id, pageNum, window.commentList);
        } else {
            $(this).html("没有更多评论了");
            return;
        }
    });
    $(".footer").on("click",function(){
        if(navigator.userAgent.indexOf("iPhone") > -1) {
            location.href = "https://itunes.apple.com/cn/app/id1234070505";
        }else if(navigator.userAgent.indexOf("Android") > -1) {
            location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=net.pusai.mall";
        }
    })
});